package com.hjx.windowtool.face;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hjx.windowtool.util.XiaoShuoTest2;
import com.hjx.windowtool.util.XiaoshuoTest3;

public class GetFace {
	public static void main(String[] args) throws Exception {
		javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
		TrustManager tm = new XiaoshuoTest3.miTM();
		trustAllCerts[0] = tm;
		javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, null);
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		List<String> list=new ArrayList<>();
		
		for (int i = 1;i<2 ; i++) {
			try {
				Connection conn = Jsoup.connect("https://avmoo.cyou/cn/actresses/page/"+i);//https://avsox.website/cn/actresses
				conn.header("User-Agent", XiaoShuoTest2.userAgent);
				Document d = conn.get();
				//		System.out.println(d);
				Elements select = d.select(".item");
				if (select==null) {
					break;
				}
				for (Element e : select) {
								System.out.println(e);
					String img = e.select("img").attr("src");
					String name = e.select("span").text();
					String info = list.size()+"==="+name + "===" + img+"==="+e.select("a").attr("href");
					System.out.println(info);
					list.add(info);
				}
//				Files.write(Paths.get("2.txt"), list);
				if (select.size()<10) {
					break;
				}
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
				i--;
			} 
		}
		
		
		
//		for (int j = 1; j < 3; j++) {
//			List<String> readAllLines = Files.readAllLines(Paths.get(j+".txt"));
//			for (int i = 0; i < readAllLines.size(); i++) {
//				try {
//					String string = readAllLines.get(i);
//					System.out.println(string);
//					String[] split = string.split("===");
//					System.out.println(split[1] + "===" + split[2]);
//					String save = "face" + File.separator + split[1] + ".jpg";
//					if (new File(save).exists()) {
//						continue;
//					}
//					XiaoShuoTest2.downloadImage(split[2], save, null);
//					Thread.sleep(1000);
//				} catch (Exception e) {
//					e.printStackTrace();
//					Thread.sleep(2000);
//				}
//			} 
//		}
	}
}
